package org.dromara.canteen.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.canteen.domain.vo.CanteenEmployeeVo;
import org.dromara.canteen.domain.bo.CanteenEmployeeBo;
import org.dromara.canteen.service.ICanteenEmployeeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 从业人员管理
 *
 * @author Gjb
 * @date 2025-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/canteen/employee")
public class CanteenEmployeeController extends BaseController {

    private final ICanteenEmployeeService canteenEmployeeService;

    /**
     * 查询从业人员管理列表
     */
    @SaCheckPermission("canteen:employee:list")
    @GetMapping("/list")
    public TableDataInfo<CanteenEmployeeVo> list(CanteenEmployeeBo bo, PageQuery pageQuery) {
        return canteenEmployeeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出从业人员管理列表
     */
    @SaCheckPermission("canteen:employee:export")
    @Log(title = "从业人员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CanteenEmployeeBo bo, HttpServletResponse response) {
        List<CanteenEmployeeVo> list = canteenEmployeeService.queryList(bo);
        ExcelUtil.exportExcel(list, "从业人员管理", CanteenEmployeeVo.class, response);
    }

    /**
     * 获取从业人员管理详细信息
     *
     * @param employeeId 主键
     */
    @SaCheckPermission("canteen:employee:query")
    @GetMapping("/{employeeId}")
    public R<CanteenEmployeeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long employeeId) {
        return R.ok(canteenEmployeeService.queryById(employeeId));
    }

    /**
     * 新增从业人员管理
     */
    @SaCheckPermission("canteen:employee:add")
    @Log(title = "从业人员管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CanteenEmployeeBo bo) {
        System.out.println("新增从业");
        return toAjax(canteenEmployeeService.insertByBo(bo));
    }

    /**
     * 修改从业人员管理
     */
    @SaCheckPermission("canteen:employee:edit")
    @Log(title = "从业人员管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CanteenEmployeeBo bo) {
        System.out.println("修改");
        return toAjax(canteenEmployeeService.updateByBo(bo));
    }

    /**
     * 删除从业人员管理
     *
     * @param employeeIds 主键串
     */
    @SaCheckPermission("canteen:employee:remove")
    @Log(title = "从业人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{employeeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] employeeIds) {
        return toAjax(canteenEmployeeService.deleteWithValidByIds(List.of(employeeIds), true));
    }
}
